﻿using Approval.API.Applcation.Command.PayRelationCommand;
using Approval.Domain;
using Approval.Domain.Common;
using Approval.Infrastructure.Interface;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Approval.API.Applcation.CommandHandler.PayRelationCommandHandler
{
    public class PayRelationApprovalCommandHandler : IRequestHandler<PayRelationApprovalCommand, Audit>
    {
        private readonly IPayRelationRepository payRelationRepository;
        private readonly Cross_services_Helper cross_Services_Helper;

        public PayRelationApprovalCommandHandler(IPayRelationRepository payRelationRepository, Cross_services_Helper cross_Services_Helper)
        {
            this.payRelationRepository = payRelationRepository;
            this.cross_Services_Helper = cross_Services_Helper;
        }

        public async Task<Audit> Handle(PayRelationApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var info = payRelationRepository.All().Result.Where(x => x.PayId == request.PayId && x.ApprovalId == request.UserId).FirstOrDefault();
                //审批时间
                info.CreateDate = DateTime.Now;
                //审批备注
                info.ApprovalDesc = request.ApprovalDesc;
                //同意
                if (request.Identifier == 1)
                {
                    info.ApprovalState = 2;
                    await payRelationRepository.Update(info);
                    var payapproval = new PayRelation()
                    {
                        PayId = request.PayId,
                        ApprovalId = request.TransferId,
                        ApprovalState = 1,
                        ApprovalName = request.TransferName,
                        ApprovalDesc = null
                    };
                    var res = await payRelationRepository.Create(payapproval);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.TransferName,
                        CreateById = request.TransferId,
                        AuditState = 1,
                        IsDel = false
                    };
                    return audit;
                }
                //拒绝
                if (request.Identifier == 2)
                {
                    info.ApprovalState = 3;
                    await payRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改状态
                    var Pay = await cross_Services_Helper.Get($"http://10.31.60.9:8000/LReadAPIRout/api/Settlement/FanPaymnet?PaymentId={request.PayId}");
                    var Pays = JsonConvert.DeserializeObject<PayDto>(Pay);
                    var PayList = new PayState();
                    PayList.paymentId = Pays.PaymentId;
                    PayList.auditState = 3;
                    Pays.Approver = "小明";
                    var res = await cross_Services_Helper.Post("http://10.31.60.9:8000/LAPIRout/api/Settlement/StateSPayment", PayList);


                    return audit;
                }
                //通过并终止转批
                if (request.Identifier == 3)
                {
                    info.ApprovalState = 2;
                    await payRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改状态
                    var Pay = await cross_Services_Helper.Get($"http://10.31.60.9:8000/LReadAPIRout/api/Settlement/FanPaymnet?PaymentId={request.PayId}");
                    var Pays = JsonConvert.DeserializeObject<PayDto>(Pay);
                    var PayList = new PayState();
                    PayList.paymentId = Pays.PaymentId;
                    PayList.auditState = 2;
                    Pays.Approver = "小明";
                    var res = await cross_Services_Helper.Post("http://10.31.60.9:8000/LAPIRout/api/Settlement/StateSPayment", PayList);
                    return audit;
                }


                return info;
            }
            catch (Exception)
            {
                return null;
                throw;
            }

            
            

        }
    }
}
